package dobest.ray.com.jiongjiong.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.bean.City;


/**
 * 城市列表的适配器
 * 
 * @Copyright Copyright © 2014 蓝色互动. All rights reserved.
 * 
 * @author ZhuFan
 * @Date: 2014-12-11
 */
@SuppressLint("InflateParams")
public class CityListAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	/**
	 * 城市列表
	 */
	private List<City> list;
	private final int VIEW_TYPE = 1;
	/**
	 * 暂存当前与前一个
	 */
	private String current, prevous, next;
	/**
	 * 存储字母索引
	 */
	private HashMap<String, Integer> alphaIndex;

	public CityListAdapter(Context context, List<City> list) {
		this.inflater = LayoutInflater.from(context);
		this.list = list;
		alphaIndex = new HashMap<String, Integer>();
		for (int i = 0; i < list.size(); i++) {
			if (i == 0) {
				alphaIndex.put(getFirstLetter(list.get(i).first), i);
			} else {
				current = getFirstLetter(list.get(i).first);
				prevous = getFirstLetter(list.get(i - 1).first);

				if (!current.equals(prevous)) {
					alphaIndex.put(current, i);
				}
			}
		}
	}
	
	

	public List<City> getList() {
		return list;
	}

	public void setList(List<City> list) {
		this.list = list;
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_select_city, null);
			vHolder = new ViewHolder();
			vHolder.alphaTextView = (TextView) convertView.findViewById(R.id.alpha);
			vHolder.nameTextView = (TextView) convertView.findViewById(R.id.name);
			vHolder.divider = convertView.findViewById(R.id.divider);
			convertView.setTag(vHolder);
		} else {
			vHolder = (ViewHolder) convertView.getTag();
		}
		vHolder.nameTextView.setText(list.get(position).name);
		
		if(position < getCount() - 1){
			next = getFirstLetter(list.get(position + 1).first);
		}
		
		if(position == 0){
			vHolder.alphaTextView.setText(getFirstLetter(list.get(position).first));
			vHolder.alphaTextView.setVisibility(View.VISIBLE);
		} else {
			current = getFirstLetter(list.get(position).first);
			prevous = getFirstLetter(list.get(position - 1).first);
			if (!current.equals(prevous)) {
				vHolder.alphaTextView.setText(current);
				vHolder.alphaTextView.setVisibility(View.VISIBLE);
			} else {
				vHolder.alphaTextView.setVisibility(View.GONE);
			}
		}
		
		if(!current.equals(next)) {
			vHolder.divider.setVisibility(View.GONE);
		} else {
			vHolder.divider.setVisibility(View.VISIBLE);
		}

		return convertView;
	}

	@Override
	public int getViewTypeCount() {// 这里需要返回需要集中布局类型，总大小为类型的种数的下标
		return VIEW_TYPE;
	}

	/**
	 * 获取拼音首字母
	 * 
	 * @author ZhuFan
	 * @Package com.bm.chengkai.adapter
	 * @param str
	 * @return
	 * @return String
	 * @throws
	 * @Date 2014-12-11 下午6:37:35
	 */
	@SuppressLint("DefaultLocale")
	private String getFirstLetter(String str) {
		char c = str.trim().substring(0, 1).charAt(0);
		// Pattern pattern = Pattern.compile("^[A-Za-z]+$");
		return (c + "").toUpperCase();
	}

	/**
	 * 控件集合
	 * 
	 * @Copyright Copyright © 2014 蓝色互动. All rights reserved.
	 * 
	 * @author ZhuFan
	 * @Date: 2014-12-11
	 */
	private class ViewHolder {
		private TextView alphaTextView;
		private TextView nameTextView;
		private View divider;
	}

	/**
	 * 获取alphaIndex
	 * 
	 * @author ZhuFan
	 * @Package com.bm.chengkai.adapter
	 * @return
	 * @return HashMap<String,Integer>
	 * @throws
	 * @Date 2014-12-11 下午6:37:21
	 */
	public HashMap<String, Integer> getAlphaIndex() {
		if (alphaIndex != null) {
			return alphaIndex;
		} else {
			return null;
		}
	}

}
